﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilZ.Dotnet.AudioPlayer.Core;
using UtilZ.Dotnet.AudioPlayer.Player;
using UtilZ.Dotnet.AudioPlayer.Recorder;
using UtilZ.Dotnet.Ex.Audio;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;

namespace WinFormApp
{
    public partial class FTestAudioPlayer : Form
    {
        public FTestAudioPlayer()
        {
            InitializeComponent();
        }



        private void FTestAudioPlayer_Load(object sender, EventArgs e)
        {
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }

            BASSHelper.Init();
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            logControl.AddLog(e.Item.Content, e.Item.Level);
        }

        private StreamSoundPlayer _soundPlayer = null;
        private void btnPlay_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._soundPlayer == null)
                {
                    var waveInfo = new WaveInfo(WaveEncoding.Pcm, 8000, 1, 16);
                    this._soundPlayer = new StreamSoundPlayer(1, waveInfo);
                    this._soundPlayer.Play();
                    Loger.Info("开始播放");
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private SoundRecorder _soundRecorder = null;
        private void btnStartRecord_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._soundRecorder == null)
                {
                    var options = new SoundRecorderOptions()
                    {
                        DeviceId = -1,
                        SampleRate = 8000,
                        Chans = 1,
                        SampleBits = DataSampleBits.BIT16
                    };

                    if (this._soundRecorder != null)
                    {
                        this._soundRecorder.Stop();
                    }
                    this._soundRecorder = new SoundRecorder(this.RecorderCallBack, options);
                }

                if (!this._soundRecorder.Recording)
                {
                    this._soundRecorder.Start();
                    Loger.Info("开始录音");
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        /// <summary>
        /// 录音回调
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="data"></param>
        /// <param name="dataLen"></param>
        unsafe private void RecorderCallBack(SoundRecorder soundRecorder, PtrSpan<byte> dataSpan)
        {
            try
            {
                if (this._soundPlayer != null)
                {
                    this._soundPlayer.AppendData(dataSpan.DataPtr, dataSpan.Length);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex, "音数据采集回调输出处理错误");
            }
        }


        private void btnStopRecord_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._soundRecorder != null && this._soundRecorder.Recording)
                {
                    this._soundRecorder.Stop();
                    Loger.Info("停止录音");
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }
    }
}
